<script setup>
import * as api from "@/api/v-sys-service/exam/template";
import { sendExamPaper } from "@/api/v-sys-service/exam/paper/index.js";
import templateFormDialog from "@/views/v-sys-service/exam/template/template-form-dialog.vue"
import paperDialog from "@/views/v-sys-service/exam/paper/index.vue"

const props = defineProps({
  rowData: {
    type: Object,
    default: () => {}
  }
})


/**
 * 列表数据
 */
const tableData = ref([])

/**
 * loading状态
 */
const loading = ref({
  tableLoading: false,// 列表loading
})

/**
 * 弹窗显隐
 */
const dialog = ref({
  showFormDialog: false, //表单弹窗
  showSendExamPaperDialog: false, //发放考卷弹窗
  showExamRankDialog: false, //考试排行榜弹窗
})

const rowData = ref({})

const examId = ref('')

/**
 * 计算表格属性
 */
const tableInfo = computed(()=>{
  const tableProps = {
    data: tableData.value,
    loading: loading.value.tableLoading,
    rowConfig: { keyField: 'id' },
    checkboxConfig: { highlight: false, reserve: true },
    showSelectCount: false,
    showExportBtn: false,
  }
  return { ...tableProps }
})

/**
 * 分页参数
 */
const pageQuery = reactive({
  page: 1,
  limit: 50,
  total: 0,
  url: '/cloud-xxx-service/exam/page',
  orders: [
    {column: "createTime", asc: false}
  ],
  filters: [],
  condition: {
    key: "",
    groupId: props.rowData.id
  },
})

/**
 * 组件初始化调用函数
 */
const init = () => {
  if (props.rowData.id) init_PageQuery()
}

/**
 * 分页查询
 */
const init_PageQuery = async () => {
  loading.value.tableLoading = true
  const { code, data } = await api.page(pageQuery)
  if (code === 200) {
    loading.value.tableLoading = false
    tableData.value = data.records
    pageQuery.total = data.total
  }
}

/**
 * form表单
 */
const showFormDialog = ($event,row) => {
  if (row && row.id) rowData.value = row
  else rowData.value = {}
  dialog.value.showFormDialog = true
  rowData.value.groupId = props.rowData.id
}

/**
 * 删除
 */
const showRemoveDialog = ($event,row) => {
  ElMessageBox.confirm('', '确定删除吗',
      {
        closeOnClickModal: false,
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
        beforeClose: (action, instance, done) => {
          if (action === 'confirm') {
            instance.confirmButtonLoading = true
            instance.confirmButtonText = '拼命加载中...'
            api.removeById(row.id).then(({code})=>{
              instance.confirmButtonLoading = false
              if (code === 200){
                ElMessage({message: '删除成功', type: 'success',})
                init_PageQuery()
              }
            })
            done()
          } else {
            done()
          }
        }
      }
  )
}

/**
 * 发放考卷
 */
const showSendExamPaperDialog = ($event,row) => {
  //
  ElMessageBox.prompt(
      "请填写参考人员姓名：",
      "发放考卷",
      {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        inputPattern: /\S/,
        inputErrorMessage: "人员名称不能为空",
      }
  )
      .then(async ({value}) => {
        const params = {
          personName: value,
          examId: row.id
        }
        const { code } = await sendExamPaper(params)
        if (code === 200) {
          ElMessage.success("操作成功！")
        }
      })
}

/**
 * 考试排行榜
 */
const showExamRankDialog = (row) => {
  dialog.value.showExamRankDialog = true
  examId.value = row.id
}

/**
 * 组件挂载后
 */
onMounted(() => {
  init()
})

/**
 * 组件对外提供可访问数据
 */
defineExpose({
  showFormDialog,
  showRemoveDialog,
})

</script>

<template>
  <div class="exam-box flex flex-col">
    <div class="btn">
      <div class="flex align-center">
        <label class="label">{{'\u2003'}}关键字{{'\u2003'}}</label>
        <el-input
            v-model="pageQuery.condition.key"
            style="width: 180px;" placeholder="请输入关键字"
            @keyup.enter="init_PageQuery" @clear="init_PageQuery" clearable
        />
        <div class="el-button" style="display: none"></div>
        <el-button type="primary" @click="init_PageQuery">
          <template #icon><i-ep-search /></template>搜索
        </el-button>
        <el-button type="primary" @click="showFormDialog">
          <template #icon><i-ep-plus /></template>创建试卷模板
        </el-button>
      </div>
    </div>
    <v-table
        v-model="pageQuery"
        :table-info="tableInfo"
        :column-info="api.columnInfo"
        @query="init_PageQuery"
    >
      <template #examStatus="{row}">
        <el-switch
            v-model="row.examStatus"
            active-color="#13ce66"
            inactive-color="#ff4949"
            :active-value="true"
            @click=""
            :inactive-value="false">
        </el-switch>
      </template>
      <template #operate="{row}">
        <div class="flex align-center">
          <el-button type="primary" :text="true" size="small" @click="showSendExamPaperDialog($event,row)">
            <template #icon><i-ep-edit /></template>发放考卷
          </el-button>
          <el-dropdown>
          <span class="el-dropdown-link">
            &emsp;更多
            <el-icon><i-ep-arrow-down /></el-icon>
          </span>
            <template #dropdown>
              <el-dropdown-menu>
                <el-dropdown-item @click="showFormDialog($event, row)">查看试卷</el-dropdown-item>
                <el-dropdown-item @click.native="showRemoveDialog($event, row)">删除试卷</el-dropdown-item>
                <el-dropdown-item @click.native="showExamRankDialog(row)">考试排行榜</el-dropdown-item>
              </el-dropdown-menu>
            </template>
          </el-dropdown>
        </div>
      </template>
    </v-table>
    <!--  试卷模板表单   -->
    <templateFormDialog v-if="dialog.showFormDialog" @query="init_PageQuery" v-model="dialog.showFormDialog" :row-data="rowData" />

    <!--  考试排行榜  -->
    <paper-dialog v-if="dialog.showExamRankDialog" v-model="dialog.showExamRankDialog" :exam-id="examId" />

  </div>
</template>

<style scoped lang="scss">
  .exam-box {
    padding: 10px;
    .btn {
      text-align: right;
      position: absolute;
      top: -35px;
      right: 0;
      padding-right: 20px;
    }
    .el-dropdown-link {
      line-height: 24px;
      font-size: 12px;
      color: #00a7ff;
      cursor: pointer;
    }
    .container {
      :deep(.el-tag) {
        cursor: pointer;
      }
      :deep(.el-button) {
        padding: 0;
      }
    }
  }
</style>
